package com.find.memories.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.find.memories.R;

public class FragmentSmsAdapter extends BaseAdapter{
    
    public static final int MESSAGE_TYPE_RECEIVE = 1;  
    public static final int MESSAGE_TYPE_SEND = 2;
    private static final String IMAGE_CACHE_DIR = "thumbs";
    
    private final Context mContext;
    private int mItemHeight = 0;
    private int mActionBarHeight = 0;
    private GridView.LayoutParams mImageViewLayoutParams;
    private ArrayList<SmsItem> mItems = new ArrayList<SmsItem>();
    private LayoutInflater mInflater;

    public FragmentSmsAdapter(Context context, ArrayList<SmsItem> items) {
        super();
        mContext = context;
        mItems = items;
        mInflater = LayoutInflater.from(mContext);
                
        mImageViewLayoutParams = new GridView.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        // Calculate ActionBar height
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(
                android.R.attr.actionBarSize, tv, true)) {
            mActionBarHeight = TypedValue.complexToDimensionPixelSize(
                    tv.data, context.getResources().getDisplayMetrics());
        }
        
    }

    @Override
    public int getCount() {
//        return mItems.size() + mNumColumns;
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
//        return position < mNumColumns ?
//                null : mItems.get(position- mNumColumns);
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
//        return position < mNumColumns ? 0 : position - mNumColumns;
        return position;
    }

    @Override
    public int getViewTypeCount() {
        // Two types of views, the normal ImageView and the top row of empty views
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.valueOf(mItems.get(position).type);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        
        ViewHolder viewHolder = new ViewHolder();
        
        SmsItem smsItem = mItems.get(position);
        
        int type = getItemViewType(position);
        
        if (convertView == null) {
            switch (type) {
                case MESSAGE_TYPE_RECEIVE:
                    convertView = mInflater.inflate(R.layout.sms_msg_receive_test, container, false);
                    break;
                case MESSAGE_TYPE_SEND:
                    convertView = mInflater.inflate(R.layout.sms_msg_send_test, container, false);
                    break;
                default:
                    break;
            }
                
            viewHolder.tvMsg = (TextView) convertView.findViewById(R.id.txt_message);
            viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
            convertView.setTag(viewHolder);
            
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        
        String date = new SimpleDateFormat("HH:mm").format(Long.valueOf(smsItem.getRegDate()));
        
        viewHolder.tvMsg.setText(smsItem.getData());
        viewHolder.tvDate.setText(date);
        
        return convertView;
    }

    public void setItems(ArrayList<SmsItem> items) {
        mItems.clear();
        mItems = items;
        notifyDataSetChanged();
    }
    
    /**
     * Sets the item height. Useful for when we know the column width so the height can be set
     * to match.
     *
     * @param height
     */
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams =
                new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        // test
//        mImageFetcher.setImageSize(height);
        notifyDataSetChanged();
    }

    public class ViewHolder {
        public TextView tvMsg;
        public TextView tvDate;
    }
    
}

